package com.sskj.level.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.level.bean.AssetListBean;
import com.sskj.level.bean.CoinUpdateBean;
import com.sskj.level.bean.TradeInfoBean;
import com.sskj.level.bean.TradeSetBean;
import com.sskj.level.http.HttpConfig;
import com.sskj.level.ui.fragment.BuyAndSellFragment;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.bean.WSFiveBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;

import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.functions.BiFunction;


public class BuyAndSellFragmentPresenter extends BasePresenter<BuyAndSellFragment> {


    public void getCoinInfo(String code) {

        Flowable<CoinBean> coinFlow = httpService.getPro(code)
                .converter(new JsonConverter<HttpData<List<CoinBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(listHttpData -> listHttpData.getData().get(0));
        Flowable<TradeSetBean> setFlow = httpService.getTradeSetInfo(code)
                .converter(new JsonConverter<HttpData<TradeSetBean>>() {
                })
                .adapt(new FlowableBody<>())
                .map(listHttpData -> listHttpData.getData());
        Flowable.zip(coinFlow, setFlow, (coinBean, tradeSetBean) -> {
            CoinUpdateBean coinUpdateBean = new CoinUpdateBean();
            TradeSetBean data = tradeSetBean;
            coinUpdateBean.setCode(data.getCode());
            coinUpdateBean.setLevels(data.getLeverage());
            coinUpdateBean.setMinDealNum(data.getNum_min());
            coinUpdateBean.setSpread(data.getSpread());
            coinUpdateBean.setSlidingScalePrice(data.getVar_price());
            coinUpdateBean.setShouPer(data.getTrans_fee());
            coinUpdateBean.setPrice(coinBean.getPrice());
            coinUpdateBean.setPaperValue(data.getPcs_price());
            coinUpdateBean.setTrans_ware(data.getTrans_ware());
            mView.updateCoinData(coinUpdateBean);
            return coinUpdateBean;
        }).subscribe(coinUpdateBean -> mView.updateCoinData(coinUpdateBean),e->{});
    }

    public void createOrder(String entrustLot, String buyPrice, String stockCode, String billPriceType,
                            String buyBillType, String stopProfit, String stopLoss, String lever) {
        httpService.createOrder(entrustLot, buyPrice, buyBillType, stockCode, billPriceType, stopProfit, stopLoss, lever)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.getMsg());

                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.buySuccess();
                        }
                    }
                });
    }

    //    public void getRisk(UserData userData) {
//        if (userData == null) {
//            mView.updateRisk(null);
//            return;
//        }
//        httpService.getTradeInfo()
//                .tag(this)
//                .execute(new JsonCallBack<HttpData<TradeInfoBean>>() {
//                    @Override
//                    public void onSuccess(Response<HttpData<TradeInfoBean>> response) {
//                        HttpData<TradeInfoBean> httpData = response.body();
//                        if (httpData.getStatus() == HttpConfig.OK) {
//                            mView.updateRisk(httpData.getData());
//                        }
//
//                    }
//                });
//    }
    public void getDepthData(String code) {
        httpService.getDeepData(code)
                .execute(new JsonCallBack<HttpData<List<WSFiveBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpData<List<WSFiveBean>>> response) {
                        HttpData<List<WSFiveBean>> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.updateDeep(httpData.getData().get(0));
                        }
                    }
                });
    }

    public void getUSDT() {
        httpService.getUsdt()
                .execute(new JsonCallBack<HttpData<AssetListBean>>() {
                    @Override
                    public void onSuccess(Response<HttpData<AssetListBean>> response) {
                        HttpData<AssetListBean> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {

                            mView.updateUSDT(httpData.getData());
                        }
                    }
                });
    }
}
